package me.xue.communityhealth.util;

import java.io.File;

import static me.xue.communityhealth.data.Constant.*;

public final class ApplicationUtil {

    private ApplicationUtil() {
    }

    public static File getBasePath() {
        String userHome = System.getProperty("user.home");
        if (userHome != null) {
            File projectPath = new File(userHome + File.separator + PROJECT_NAME);
            if (!projectPath.exists()) {
                if (!projectPath.mkdirs()) {
                    throw new RuntimeException("无法创建项目目录");
                }
            }
            return projectPath;
        }
        return new File("." + File.separator + PROJECT_NAME);
    }

    public static File getFilePath() {
        File basePath = getBasePath();
        File filesDir = new File(basePath.getAbsolutePath() + File.separator + FILES_DIR);
        if (!filesDir.exists()) {
            if (!filesDir.mkdirs())
                throw new RuntimeException("无法创建文件目录");
        }
        return filesDir;
    }

    public static File getPortraitPath() {
        File fileDir = getFilePath();
        File portraitDir = new File(fileDir.getAbsolutePath() + File.separator + PORTRAIT_DIR);
        if (!portraitDir.exists()) {
            if (!portraitDir.mkdirs())
                throw new RuntimeException("无法创建头像文件目录");
        }
        return portraitDir;
    }

    public static File getDrugPath() {
        File fileDir = getFilePath();
        File drugsDir = new File(fileDir.getAbsolutePath() + File.separator + DRUGS_DIR);
        if (!drugsDir.exists()) {
            if (!drugsDir.mkdirs())
                throw new RuntimeException("无法创建药品图片文件目录");
        }
        return drugsDir;
    }

}
